from listnode import *


class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode):
        flag = 0
        head = tail = ListNode(-1)
        while l1 and l2:
            ans = l1.val + l2.val + flag
            flag = ans // 10
            tail.next = ListNode(ans % 10)
            tail = tail.next
            l1 = l1.next
            l2 = l2.next

        while l1:
            ans = l1.val + flag
            flag = ans // 10
            tail.next = ListNode(ans % 10)
            tail = tail.next
            l1 = l1.next

        while l2:
            ans = l2.val + flag
            flag = ans // 10
            tail.next = ListNode(ans % 10)
            tail = tail.next
            l2 = l2.next

        if flag:
            tail.next = ListNode(flag)
            tail = tail.next

        return head.next


Solution().addTwoNumbers(create_list([5]), create_list([5]))
